package com.mymail.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import com.mymail.entity.Mail;

public class MailDAO extends DAOImp implements DAO {
	
	/**
	 * 调用存储过程 delete_mail()安全删除一组邮件，并保证拉圾邮件在五十个左右
	 * @param str 要删除的邮件的ID用;号分开
	 * @param uid 邮件所属用户
	 * @return
	 */
	public boolean delete1(String str,Integer uid){
		String sql="{ call delete_mail(?,?) }";
		return exeProcedure(sql, new Object[]{str,uid});
	}
	
	/**
	 * 调用存储过程 delete2_mail()彻底删除一组邮件
	 * @param str 要删除的邮件的ID用;号分开
	 * @param uid 邮件所属用户
	 * @return
	 */
	public boolean delete2(String str,Integer uid){
		String sql="{ call delete2_mail(?,?) }";
		return exeProcedure(sql, new Object[]{str,uid});
	}

	public ArrayList findMailsByFolderId(Integer folder_id, Integer start,
			Integer end) {
		String sql = "select * from `mail` where `folder_id`=? order by mail_sendtime desc limit ?,?";
		return findArray(sql, new Object[] { folder_id, start, end });
	}

	public boolean getByID(Integer mail_id, Object obj) {
		Mail mail = (Mail) obj;
		String sql = "select * from `mail` where `mail_id`=?";
		return findOne(sql, new Object[] { mail_id }, mail);
	}

	public boolean delete(Object obj) {
		Mail mail = (Mail) obj;
		String sql = "delete from `mail` where mail_id=?";
		return exe(sql, new Object[] { mail.getId() });
	}

	public boolean toDatabase(Object obj) {
		Mail mail = (Mail) obj;
		if (mail.getId() == null) {
			return addNew(mail);
		} else {
			return update(mail);
		}
	}

	public boolean update(Object obj) {
		Mail mail = (Mail) obj;
		char[] c = { mail.getSort() };
		String sql = "update `mail` set folder_id=?,mail_subject=?,"
				+ "mail_content=?,mail_from=?,mail_sort=?,mail_per_name=?,"
				+ "mail_sendtime=? where mail_id=?";
		return exe(sql, new Object[] { mail.getFolder(), mail.getSubject(),
				mail.getContent(), mail.getFrom(), new String(c),
				mail.getPer_name(), mail.getSendTime(), mail.getId() });
	}

	public boolean addNew(Object obj) {
		Mail mail = (Mail) obj;
		char[] c = { mail.getSort() };
		String sql = "insert into `mail` (folder_id,mail_subject,"
				+ "mail_content,mail_from,mail_sort,mail_per_name,"
				+ "mail_sendtime) values (?,?,?,?,?,?,?)";
		int i = insert(sql, new Object[] { mail.getFolder(), mail.getSubject(),
				mail.getContent(), mail.getFrom(), new String(c),
				mail.getPer_name(), mail.getSendTime() });
		if (i > 0) {
			mail.setId(i);
			return true;
		}
		return false;
	}

	protected Mail getEntity(ResultSet result) throws SQLException {
		Mail mail = new Mail();
		setAtribute(result, mail);
		return mail;
	}

	protected void setAtribute(ResultSet result, Object obj)
			throws SQLException {
		Mail mail = (Mail) obj;
		mail.setId(result.getInt("mail_id"));
		mail.setFolder(result.getInt("folder_id"));
		mail.setFrom(result.getString("mail_from"));
		mail.setContent(result.getString("mail_content"));
		mail.setSubject(result.getString("mail_subject"));
		mail.setSort((char) result.getString("mail_sort").getBytes()[0]);
		mail.setSendTime(result.getTimestamp("mail_sendtime"));
		mail.setPer_name(result.getString("mail_per_name"));
		mail.setReceiver(new ReceiverDAO().getReceivers(mail.getId()));
	}

}
